package jdos.gui;

import jdos.ints.Mouse;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.*;

public class Main extends MainBase {
    static public void GFX_SetCursor(Cursor cursor) {

    }
    static public void GFX_Events() {
            while (events.size()>0) {
                Object event = events.elementAt(0);
                events.removeElementAt(0);
                if (event instanceof FocusChangeEvent)
                    handle((FocusChangeEvent)event);
                else if (event instanceof MouseEvent)
                    handle((MouseEvent)event);
            }
        }

    static public class MouseEvent2 extends MouseEvent {
            public int rel_x;
            public int rel_y;
            public float abs_x;
            public float abs_y;
            public MouseEvent2(MouseEvent event, int rel_x, int rel_y, float abs_x, float abs_y, int offX, int offY) {
                super(event.getComponent(), event.getID(), event.getWhen(), event.getModifiers(), event.getX()-offX, event.getY()-offY, event.getClickCount(), event.isPopupTrigger());
                this.rel_x = rel_x;
                this.rel_y = rel_y;
                this.abs_x = abs_x;
                this.abs_y = abs_y;
            }
        }
        static public class MouseEvent1 extends MouseEvent {
            public MouseEvent1(MouseEvent event, int offX, int offY) {
                super(event.getComponent(), event.getID(), event.getWhen(), event.getModifiers(), event.getX()-offX, event.getY()-offY, event.getClickCount(), event.isPopupTrigger());
            }
        }
        static private Point lastMouse = new Point();
        static private void handle(MouseEvent event) {
            if (event.getID() == MouseEvent.MOUSE_MOVED || event.getID() == MouseEvent.MOUSE_DRAGGED) {
                // :TODO: test relative mouse with lucas arts games (indy3, indy4)
                if (mouse_locked || !mouse_autoenable) {
                    if (event instanceof MouseEvent2) {
                        MouseEvent2 event2 = (MouseEvent2)event;
                        Mouse.Mouse_CursorMoved((float) event2.rel_x * mouse_sensitivity / 100.0f,
                                (float) (event2.rel_y) * mouse_sensitivity / 100.0f,
                                event2.abs_x,
                                event2.abs_y,
                                mouse_locked);
                    } else {
                        Mouse.Mouse_CursorMoved((float)(event.getPoint().x-lastMouse.x)*mouse_sensitivity/100.0f,
                                  (float)(event.getPoint().y-lastMouse.y)*mouse_sensitivity/100.0f,
                                  (float)event.getPoint().x/(screen_width-1)*mouse_sensitivity/100.0f,
                                  (float)event.getPoint().y/(screen_height-1)*mouse_sensitivity/100.0f,
                                  mouse_locked);
                        lastMouse = event.getPoint();
                    }
                }
            } else if (event.getID() == MouseEvent.MOUSE_PRESSED) {
                if (mouse_requestlock && !mouse_locked) {
                    GFX_CaptureMouse();
                    // Dont pass klick to mouse handler
                    return;
                }
                if (!mouse_autoenable && mouse_autolock && event.getButton() == MouseEvent.BUTTON3) {
                    GFX_CaptureMouse();
                    return;
                }
                if (event.getButton() == MouseEvent.BUTTON1) {
                    Mouse.Mouse_ButtonPressed(0);
                } else if (event.getButton() == MouseEvent.BUTTON2) {
                    Mouse.Mouse_ButtonPressed(2);
                } else if (event.getButton() == MouseEvent.BUTTON3) {
                    Mouse.Mouse_ButtonPressed(1);
                }
            } else if (event.getID() == MouseEvent.MOUSE_RELEASED) {
                if (event.getButton() == MouseEvent.BUTTON1) {
                    Mouse.Mouse_ButtonReleased(0);
                } else if (event.getButton() == MouseEvent.BUTTON2) {
                    Mouse.Mouse_ButtonReleased(2);
                } else if (event.getButton() == MouseEvent.BUTTON3) {
                    Mouse.Mouse_ButtonReleased(1);
                }
            }
        }

    static public interface KeyboardHandler {
            public void handle(KeyEvent key);
        }

        static public void addEvent(Object o) {
                events.add(o);
        }

        static public interface MouseHandler {
            public void handle(MouseEvent event);
        }
        static public MouseHandler defaultMouseHandler;

        static public void addMouseEvent(MouseEvent event) {
            if (defaultMouseHandler != null)
                defaultMouseHandler.handle(event);
            else
                events.add(event);
        }


    static public void GFX_EndUpdate() {

        gui.dopaint();
    }
    public static int[] byte_rawImageData2;

    static int screen_width = 640;
    static int screen_height = 480;
    public final static int buffer_width = 640;
    final static int buffer_height = 480;
    static IndexColorModel colorModel;
    public static int[] cmap = new int[16];

    static BufferedImage buffer2;// = new BufferedImage[2];

    static public /*Bitu*/void GFX_SetSize() {

        gui.setSize();
        updateBuffers();
    }

    static public void updateBuffers() {

        buffer2 = new BufferedImage(buffer_width, buffer_height, BufferedImage.TYPE_INT_RGB);
//            buffer2[1] = new BufferedImage(buffer_width, buffer_height, BufferedImage.TYPE_INT_RGB);
        DataBufferInt buf = (DataBufferInt) buffer2.getRaster().getDataBuffer();
        byte_rawImageData2 = buf.getData();
//            buf = (DataBufferInt) buffer2[1].getRaster().getDataBuffer();
//            byte_rawImageData2[1] = buf.getData();

    }

}
